package com.bwie.service.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.Customer;
import com.bwie.service.CustomerService;
import com.bwie.mapper.CustomerMapper;
import com.bwie.utils.R;
import com.bwie.vo.LoginInfoVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.TimeUnit;


/**
* @author Yoo19
* @description 针对表【customer】的数据库操作Service实现
* @createDate 2025-07-10 17:14:29
*/
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer>
    implements CustomerService{
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public R sendCode(String mobile) {
        boolean i = Validator.isMobile(mobile);
        if(!i){
            return R.ERROR(500,"手机号格式错误");
        }
        //手机号查询用户是否存在
        Customer customer = customerMapper.selectOne(new LambdaQueryWrapper<Customer>().eq(Customer::getUserMobile, mobile));
        if(customer == null){
            //如果不存在直接注册道客户中
             customer = new Customer();
             customer.setUserMobile(mobile);
             customer.setCreateTime(new Date());

             customerMapper.insert(customer);
        }

        //生成验证码
        String code = RandomUtil.randomNumbers(4);
        System.out.println("验证码为：" + code);

        String key = "login-" + mobile;
        redisTemplate.opsForValue().set(key,code,5, TimeUnit.MINUTES);
        return R.OK();
    }

    @Override
    public R login(LoginInfoVo loginInfoVo) {
        //判断验证码是否正确
        String key = "login-" + loginInfoVo.getUserMobile();
        String redisCode = redisTemplate.opsForValue().get(key);
        if(redisCode==null){
            return R.ERROR(500,"验证码不存在");
        }
        if(!redisCode.equals(loginInfoVo.getUserCode())){
            return R.ERROR(500,"验证码错误");
        }

        //查询用户是否存在
        Customer customer = customerMapper.selectOne(new LambdaQueryWrapper<Customer>().eq(Customer::getUserMobile, loginInfoVo.getUserMobile()));
        if (customer==null){
            return R.ERROR(500,"用户不存在");
        }

        String redisKey="token-"+customer.getId();
        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "123456")
                .claim("customerId", customer.getId())
                .compact();
        redisTemplate.opsForValue().set(redisKey,token,60,TimeUnit.MINUTES);


        customer.setPassword(null);
        customer.setToken(token);

        return R.OK(customer);
    }
}




